<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item :label="t('ustcStudentInfoFamily.fields.accountId')" prop="accountId">
        <el-input
          v-model="queryParams.accountId"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.accountId')
          "
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
          :disabled="advancedSearching"
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoFamily.fields.sort')" prop="sort">
        <el-input
          v-model="queryParams.sort"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.sort')"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
          :disabled="advancedSearching"
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoFamily.fields.name')" prop="name">
        <el-input
          v-model="queryParams.name"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.name')"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
          :disabled="advancedSearching"
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoFamily.fields.gender')" prop="gender">
        <el-input
          v-model="queryParams.gender"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.gender')"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
          :disabled="advancedSearching"
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoFamily.fields.relationship')" prop="relationship">
        <el-input
          v-model="queryParams.relationship"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.relationship')
          "
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
          :disabled="advancedSearching"
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoFamily.fields.unitName')" prop="unitName">
        <el-input
          v-model="queryParams.unitName"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.unitName')
          "
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
          :disabled="advancedSearching"
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoFamily.fields.career')" prop="career">
        <el-input
          v-model="queryParams.career"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.career')"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
          :disabled="advancedSearching"
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoFamily.fields.position')" prop="position">
        <el-input
          v-model="queryParams.position"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.position')
          "
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
          :disabled="advancedSearching"
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoFamily.fields.telephone')" prop="telephone">
        <el-input
          v-model="queryParams.telephone"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.telephone')
          "
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
          :disabled="advancedSearching"
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoFamily.fields.address')" prop="address">
        <el-input
          v-model="queryParams.address"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.address')
          "
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
          :disabled="advancedSearching"
        />
      </el-form-item>
      <el-form-item
        :label="t('ustcStudentInfoFamily.fields.politicalStatus')"
        prop="politicalStatus"
      >
        <el-select
          v-model="queryParams.politicalStatus"
          :placeholder="
            t('common.selectTextPlaceholder') + t('ustcStudentInfoFamily.fields.politicalStatus')
          "
          clearable
          class="!w-240px"
          :disabled="advancedSearching"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoFamily.fields.identityType')" prop="identityType">
        <el-select
          v-model="queryParams.identityType"
          :placeholder="
            t('common.selectTextPlaceholder') + t('ustcStudentInfoFamily.fields.identityType')
          "
          clearable
          class="!w-240px"
          :disabled="advancedSearching"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoFamily.fields.identityNumber')" prop="identityNumber">
        <el-input
          v-model="queryParams.identityNumber"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.identityNumber')
          "
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
          :disabled="advancedSearching"
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoFamily.fields.guardian')" prop="guardian">
        <el-select
          v-model="queryParams.guardian"
          :placeholder="
            t('common.selectTextPlaceholder') + t('ustcStudentInfoFamily.fields.guardian')
          "
          clearable
          class="!w-240px"
          :disabled="advancedSearching"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoFamily.fields.createTime')" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          :start-placeholder="t('common.startTimeText')"
          :end-placeholder="t('common.endTimeText')"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
          :disabled="advancedSearching"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery" :disabled="advancedSearching">
          <Icon icon="ep:search" class="mr-5px" />
          {{ t('common.search') }}
        </el-button>
        <el-button @click="resetQuery" :disabled="advancedSearching">
          <Icon icon="ep:refresh" class="mr-5px" />
          {{ t('common.reset') }}
        </el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['core:ustc-student-info-family:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" />
          {{ t('action.add') }}
        </el-button>
        <el-button
          type="warning"
          plain
          @click="handleImport"
          v-hasPermi="['core:ustc-student-info-family:export']"
        >
          <Icon icon="ep:upload" class="mr-5px" />
          {{ t('action.import') }}
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['core:ustc-student-info-family:export']"
        >
          <Icon icon="ep:download" class="mr-5px" />
          {{ t('action.export') }}
        </el-button>
        <ConditionTree
          id="studentinfofamily-condition-tree"
          :schemata="studentInfoFamilySchema"
          @search="handleAdvanceSearch"
        />
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <vxe-toolbar ref="toolbarRef" custom />
    <vxe-table
      id="iStudentInfoFamily-table"
      ref="tableRef"
      v-loading="loading"
      :data="list"
      stripe
      :custom-config="vxeTableConfig"
      :column-config="{ resizable: true }"
      :size="vxeTableSize"
    >
      <vxe-column :title="t('common.index')" type="seq" field="seq" width="60" align="center" />
      <vxe-column
        :title="t('ustcStudentInfoFamily.fields.id')"
        field="id"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('ustcStudentInfoFamily.fields.accountId')"
        field="accountId"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('ustcStudentInfoFamily.fields.sort')"
        field="sort"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('ustcStudentInfoFamily.fields.name')"
        field="name"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('ustcStudentInfoFamily.fields.gender')"
        field="gender"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('ustcStudentInfoFamily.fields.relationship')"
        field="relationship"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('ustcStudentInfoFamily.fields.unitName')"
        field="unitName"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('ustcStudentInfoFamily.fields.career')"
        field="career"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('ustcStudentInfoFamily.fields.position')"
        field="position"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('ustcStudentInfoFamily.fields.telephone')"
        field="telephone"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('ustcStudentInfoFamily.fields.address')"
        field="address"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('ustcStudentInfoFamily.fields.politicalStatus')"
        field="politicalStatus"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('ustcStudentInfoFamily.fields.identityType')"
        field="identityType"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('ustcStudentInfoFamily.fields.identityNumber')"
        field="identityNumber"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('ustcStudentInfoFamily.fields.guardian')"
        field="guardian"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('ustcStudentInfoFamily.fields.createTime')"
        field="createTime"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
        :formatter="vxeDateFormatter"
      />
      <vxe-column
        :title="t('common.operate')"
        field="operator"
        align="center"
        fixed="right"
        width="140"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['core:ustc-student-info-family:update']"
          >
            {{ t('action.edit') }}
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['core:ustc-student-info-family:delete']"
          >
            {{ t('action.delete') }}
          </el-button>
        </template>
      </vxe-column>
    </vxe-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <StudentInfoFamilyForm ref="formRef" @success="getList" />
  <ExcelImportForm
    ref="importFormRef"
    object-name="学生家庭成员信息"
    upload-url="/core/student-info-family/import"
    template-url="/core/student-info-family/get-import-template"
    @success="getList"
  />
</template>

<script setup lang="ts">
import { vxeDateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { UstcStudentInfoFamilyApi, UstcStudentInfoFamilyVO } from '@/api/core/ustcstudentinfofamily'
import StudentInfoFamilyForm from './StudentInfoFamilyForm.vue'
import ExcelImportForm from '@/components/ExcelImportForm/index.vue'
import type { VxeTablePropTypes, VxeToolbarInstance, VxeTableInstance } from 'vxe-table'
import { studentInfoFamilySchema } from '@/views/core/ustcstudentinfofamily/schema'
import { useAppStore } from '@/store/modules/app'

/** 学生家庭成员信息 列表 */
defineOptions({ name: 'StudentInfoFamily' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const toolbarRef = ref<VxeToolbarInstance>()
const tableRef = ref<VxeTableInstance>()

const loading = ref(true) // 列表的加载中
const list = ref<UstcStudentInfoFamilyVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const advancedSearching = ref(false) // 高级搜索
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  advanced: undefined,
  accountId: undefined,
  sort: undefined,
  name: undefined,
  gender: undefined,
  relationship: undefined,
  unitName: undefined,
  career: undefined,
  position: undefined,
  telephone: undefined,
  address: undefined,
  politicalStatus: undefined,
  identityType: undefined,
  identityNumber: undefined,
  guardian: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const vxeTableConfig: VxeTablePropTypes.CustomConfig = {
  storage: true,
  mode: 'drawer',
  checkMethod({ column }) {
    return !['seq', 'operator'].includes(column.field)
  }
}

const appStore = useAppStore()
const vxeTableSize = computed(() => appStore.getCurrentSizeVxe)

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await UstcStudentInfoFamilyApi.getUstcStudentInfoFamilyPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  advancedSearching.value = false
  ;(queryParams as any).advanced = undefined
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref<InstanceType<typeof StudentInfoFamilyForm>>()
const openForm = (type: string, id?: number) => {
  formRef.value?.open(type, id)
}

/** 导入 */
const importFormRef = ref<InstanceType<typeof ExcelImportForm>>()
const handleImport = () => {
  importFormRef.value?.open()
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await UstcStudentInfoFamilyApi.deleteUstcStudentInfoFamily(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await UstcStudentInfoFamilyApi.exportUstcStudentInfoFamily(queryParams)
    download.excel(data, '学生家庭成员信息.xlsx')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

const handleAdvanceSearch = (base64Advanced?: string) => {
  ;(queryParams as any).advanced = base64Advanced
  advancedSearching.value = !!base64Advanced
  handleQuery()
}

/** 初始化 **/
onMounted(() => {
  getList()

  const table = tableRef.value
  const toolbar = toolbarRef.value
  if (table && toolbar) {
    table.connect(toolbar)
  }
})
</script>
